package hpasDAO;

import hpasDataEntities.Hardware;
import hpasDataEntities.HardwareItem;
import hpasDataEntities.Retailer;

import javax.swing.*;
import java.sql.*;
import java.util.Vector;

public class RetailerDAO
{
    private Connection conn;
    private HardwareItemDAO hardwareItemDAO;

    /** 创建数据库访问接口 **/
    public RetailerDAO()
    {
        try
        {
            connect();
        }
        catch (SQLException e)
        {
            JOptionPane.showMessageDialog(null,"数据库建立连接失败",
                    "错误",JOptionPane.ERROR_MESSAGE);
        }
        catch (ClassNotFoundException e)
        {
            JOptionPane.showMessageDialog(null,"找不到MySQL驱动程序",
                    "错误",JOptionPane.ERROR_MESSAGE);
        }
    }
    /** 创建连接 **/
    private void connect() throws SQLException, ClassNotFoundException
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hpas","root", "niuniu53");
    }

    /** 绑定商品获取接口 **/
    public void setHardwareItemDAO(HardwareItemDAO hardwareItemDAO)
    {
        this.hardwareItemDAO = hardwareItemDAO;
    }

    /** 添加商户 **/
    public void addRetailer(Retailer retailer) throws SQLException
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate("insert into retailer (id, name, howToRetail, address) values (" +
                retailer.getId() + "," +
                "\'" + retailer.getName() + "\'," +
                "\'" + retailer.getHowToRetail() + "\'," +
                "\'" + retailer.getAddress() + "\')");
    }

    /** 根据ID查找商户 **/
    public Retailer getRetailer(int id) throws SQLException
    {
        Statement statement = conn.createStatement();
        ResultSet resultSet = statement.executeQuery(
                "select id, name, howToRetail, address from retailer where id = "+id);
        Retailer result = new Retailer();
        resultSet.next();
        result.setId(resultSet.getInt(1));
        result.setName(resultSet.getString(2));
        result.setHowToRetail(resultSet.getString(3));
        result.setAddress(resultSet.getString(4));
        return result;
    }

    /** 删除商户 **/
    public void removeRetailer(Retailer retailer) throws SQLException
    {
        Statement statement = conn.createStatement();
        statement.executeUpdate("delete from retailer where id="+retailer.getId());
    }

    /** 根据参数中的Retailer对象更新商户 **/
    public void updateRetailer(Retailer newRetailer) throws SQLException
    {
        Statement statement = conn.createStatement();
        Retailer retailerToUpdate = getRetailer(newRetailer.getId());
        statement.executeUpdate("update retailer set " +
                "name=\'" + newRetailer.getName() + "\'," +
                "howToRetail=\'" + newRetailer.getHowToRetail() + "\'," +
                "address=\'" + newRetailer.getAddress() + "\' where id=" +
                + retailerToUpdate.getId() +")");
    }

    /** 查找商户当前销售的所有硬件 **/
    public Vector<HardwareItem> getSellingItems(int retailerId) throws SQLException
    {
        Vector<HardwareItem> result = new Vector<HardwareItem>();
        Statement statement = conn.createStatement();
        ResultSet resultSet =
                statement.executeQuery("select itemId from hardwareitem where retailerId="+retailerId);
        while (resultSet.next())
        {
            HardwareItem item = hardwareItemDAO.locateHardwareItemById(resultSet.getInt(1));
            result.add(item);
        }
        return result;
    }
}
